/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
 * 
 * This file is part of the OpenWGA server platform.
 * 
 * OpenWGA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * In addition, a special exception is granted by the copyright holders
 * of OpenWGA called "OpenWGA plugin exception". You should have received
 * a copy of this exception along with OpenWGA in file COPYING.
 * If not, see <http://www.openwga.com/gpl-plugin-exception>.
 * 
 * OpenWGA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with OpenWGA in file COPYING.
 * If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

package de.innovationgate.webgate.api;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import de.innovationgate.utils.UIDGenerator;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.webgate.api.WGPortlet;

/**
 * Implementation for a transient portlet, registered only for a single request in memory, used by the {@link WGTransientPortletRegistry}
 */
public class WGTransientPortlet implements WGPortlet, Serializable {

    private static final long serialVersionUID = 1L;
    
    public Map<String,WGTransientPortlet> _childPortlets = new ConcurrentHashMap<String, WGTransientPortlet>();
    private WGTransientPortlet _parentPortlet;
    private String _name;
    private String _appDb;
    private String _designDb;
    private String _design;
    private String _key = null;
    private WGTransientPortletRegistry _registry;
    private transient WGPortlet _persistentPortlet = null;
    
    public WGTransientPortlet(WGTransientPortletRegistry registry, String appDb, WGTransientPortlet parentPortlet) {
        _registry = registry;
        _parentPortlet = parentPortlet;
        _appDb = appDb;
    }
    
    /**
     * For root portlets with direct key supply
     * @param registry The portlet registry
     * @param appDb The application of the portlet
     * @param key The key of the portlet
     */
    public WGTransientPortlet(WGTransientPortletRegistry registry, String appDb, String key) {
        _registry = registry;
        _parentPortlet = null;
        _appDb = appDb;
        _key = key;
    }
    
    /**
     * For root portlets without key supply
     * @param registry The portlet registry
     * @param appDb The application of the portlet
     */
    public WGTransientPortlet(WGTransientPortletRegistry registry, String appDb) {
        _registry = registry;
        _parentPortlet = null;
        _appDb = appDb;
    }

    /* (non-Javadoc)
     * @see de.innovationgate.webgate.api.PortletItemStorageData#getKey()
     */
    @Override
    public String getKey() {

        if (_key == null) {
            _key = generateKey(getApplicationDb(), getParentPortletKey(), getName());
        }
        
        return _key;
        
    }

    /**
     * Generates the portlet key for a transient portlet with the given specs.
     * For transient portlets the portlet key is generated by the names of involved portlets and the application id.
     * Portlets with equal name path from the same application represent the same portlet and therefor have the same portlet key.
     * @param appDb The application id of the portlet application, as given by {@link WGPortletRegistry#getApplicationId(WGDatabase)}
     * @param parentPortletKey The key of the parent portlet. null if the key should be generated for a root portlet.
     * @param name The name of the porlet. null if the key should be generated for a root portlet.
     * @return The portlet key for the given specs.
     */
    public static String generateKey(String appDb, String parentPortletKey, String name) {
        List<String> hashNames = new ArrayList<String>();
        if (parentPortletKey == null) {
            hashNames.add(appDb);
        }
        else {
            if (name == null) {
                throw new IllegalStateException("The name of this portlet has not yet been set");
            }
            hashNames.add(parentPortletKey);
            hashNames.add(name);
        }
        
        try {
            return WGUtils.createMD5HEX(WGUtils.serializeCollection(hashNames, "/").getBytes("UTF-8"));
        }
        catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("MD5 algorithm not available", e);
        }
        catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("UTF-8 encoding not available", e);
        }
    }

    /**
     * Returns the immediate child portlets of this portlet
     */
    public Map<String, WGTransientPortlet> getChildPortlets() {
        return _childPortlets;
    }

    /**
     * Returns the parent portlet of this portlet, null if it is the root portlet
     */
    public WGTransientPortlet getParentPortlet() {
        return _parentPortlet;
    }

    /* (non-Javadoc)
     * @see de.innovationgate.webgate.api.PortletItemStorageData#getName()
     */
    @Override
    public String getName() {
        return _name;
    }

    /* (non-Javadoc)
     * @see de.innovationgate.webgate.api.PortletItemStorageData#getApplicationDb()
     */
    @Override
    public String getApplicationDb() {
        return _appDb;
    }

    public String getDesign() {
        return _design;
    }

    @Override
    public String getDesignDb() {
        return _designDb;
    }

    @Override
    public void setDesignDb(String db) {
        _designDb = db;
    }

    @Override
    public void setDesign(String design) {
        _design = design;
    }

    @Override
    public void setName(String name) {
        _name = name;
    }

    @Override
    public String getParentPortletKey() {
        return (_parentPortlet != null ? _parentPortlet.getKey() : null);
    }

    @Override
    public boolean isRoot() {
        return (_parentPortlet == null);
    }
    

}
